﻿using log4net;
using McsUtility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MinerManager.Forms
{
	public partial class MinerLog : Form
	{
		ILog logger = LogManager.GetLogger(typeof(Program));
		private Common.AgentComponent Agent = null;

		private int MinerId { get; set; }
		private string MinerName { get; set; }

		private int TotalCount = 0;//数据总量
		private int PageIndex = 1;//页码
		private int PageSize = 20;//显示数量
		private int PageCount = 1;//总页码

		public MinerLog(Common.AgentComponent agent, int minerId, string minerName)
		{
			InitializeComponent();
			this.Agent = agent;
			this.MinerId = minerId;
			this.MinerName = minerName;
		}

		#region 窗体事件

		private void MinerLog_Load(object sender, EventArgs e)
		{
			this.Text = "矿机“" + this.MinerName + "”的日志";
			this.Agent.OnNodeServerMinerManagerGetLogs += new EventHandler<ByteEventArgs>(Agent_OnNodeServerMinerManagerGetLogs);

			//列表行高
			ImageList image = new ImageList();
			image.ImageSize = new Size(1, 20);
			TableList.SmallImageList = image;

			Search(1, this.PageSize);
			ShowPageMessage();
		}
		
		private void MinerLog_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.Agent.OnNodeServerMinerManagerGetLogs -= new EventHandler<ByteEventArgs>(Agent_OnNodeServerMinerManagerGetLogs);
		}

		#endregion

		#region void Agent_OnNodeServerMinerManagerGetLogs(object sender, ByteEventArgs e)

		private void Agent_OnNodeServerMinerManagerGetLogs(object sender, ByteEventArgs e)
		{
			McsUtility.Models.MinerLogsResponseModel result = ProtocolHelper.ReadPackageBody<McsUtility.Models.MinerLogsResponseModel>(e.Bytes);
			this.TotalCount = result.TotalRows;
			this.PageCount = result.TotalRows % this.PageSize > 0 ? result.TotalRows / this.PageSize + 1 : result.TotalRows / this.PageSize;

			LoadDatabase(result.Logs);
			ShowPageMessage();
		}

		#endregion

		#region TableList按钮事件

		private void BtnFirstPage_Click(object sender, EventArgs e)
		{
			if (this.PageIndex > 1)
			{
				this.PageIndex = 1;

				ShowPageMessage();
				Search(this.PageIndex, this.PageSize);
			}
		}

		private void BtnBeforPage_Click(object sender, EventArgs e)
		{
			if (this.PageIndex > 1)
			{
				this.PageIndex -= 1;
				this.PageIndex = this.PageIndex >= 1 ? this.PageIndex : 1;

				ShowPageMessage();
				Search(this.PageIndex, this.PageSize);
			}
		}

		private void BtnAfterPage_Click(object sender, EventArgs e)
		{
			if (this.PageIndex < this.PageCount)
			{
				this.PageIndex += 1;
				this.PageIndex = this.PageIndex <= this.PageCount ? this.PageIndex : this.PageCount;

				ShowPageMessage();
				Search(this.PageIndex, this.PageSize);
			}
		}

		private void BtnLastPage_Click(object sender, EventArgs e)
		{
			if (this.PageIndex < this.PageCount)
			{
				this.PageIndex = this.PageCount;

				ShowPageMessage();
				Search(this.PageIndex, this.PageSize);
			}
		}

		#endregion

		#region void TableList_MouseDoubleClick(object sender, MouseEventArgs e)

		private void TableList_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			ListView.SelectedListViewItemCollection items = TableList.SelectedItems;
			if (items.Count > 0)
			{
				McsUtility.Models.MinerLogsModel info = items[0].Tag as McsUtility.Models.MinerLogsModel;
				if (info != null)
				{
					MessageBox.Show(info.Logs, "日志");
				}
			}
		}

		#endregion



		#region void Search(int pageIndex, int pageSize)

		private void Search(int pageIndex, int pageSize)
		{
			//发送数据
			if (this.Agent.IsStarted)
			{
				McsUtility.Models.MinerLogsRequestModel info = new McsUtility.Models.MinerLogsRequestModel
				{
					MinerID = this.MinerId,
					PageIndex = pageIndex,
					PageRows = pageSize
				};
				byte[] bytes = ProtocolHelper.WritePackage<McsUtility.Models.MinerLogsRequestModel>(ProtoType.MinerManagerGetLogs, info);
				this.Agent.AgentSend(bytes);
			}
			else
			{
				MessageBox.Show("与服务器的链接断开，请重新登录系统。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}
		}

		#endregion

		#region void ShowPageMessage() 显示分页信息。

		private delegate void DelegateShowPageMessage();

		/// <summary>
		/// 显示分页信息。
		/// </summary>
		private void ShowPageMessage()
		{
			if (this.InvokeRequired)
			{
				DelegateShowPageMessage d = new DelegateShowPageMessage(ShowPageMessage);
				this.BeginInvoke(d);
			}
			else
			{
				PageMessage.Text = string.Format("共有{0}条记录 第{1}页/共{2}页", this.TotalCount, this.PageIndex, this.PageCount);
			}
		}

		#endregion

		#region void LoadDatabase(List<McsUtility.Models.MinerLogsModel> infos)

		private delegate void DelegateLoadDatabase(List<McsUtility.Models.MinerLogsModel> infos);

		private void LoadDatabase(List<McsUtility.Models.MinerLogsModel> infos)
		{
			if (this.InvokeRequired)
			{
				DelegateLoadDatabase d = new DelegateLoadDatabase(LoadDatabase);
				this.BeginInvoke(d, infos);
			}
			else
			{
				TableList.Items.Clear();

				for (int i = 0; i < infos.Count; i++)
				{
					string[] array = new string[] {
						(this.PageCount * (this.PageIndex - 1) + 1 + i).ToString(), infos[i].CreateTime.ToString("yyyy-MM-dd HH:mm:ss"), infos[i].Logs
					};

					ListViewItem item = new ListViewItem(array) { UseItemStyleForSubItems = false, Tag = infos[i] };
					TableList.Items.Add(item);
				}
			}
		}

		#endregion
	}
}
